//
//  SpriteCacheManager.m
//  CandyJump
//
//  Created by Bon Lam on 08/03/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "SpriteSheetRes.h"

#import "CCSpriteFrameCache.h"
#import "CCSpriteBatchNode.h"
#import "PRFile.h"

//---------------------------------------------------------------------------------------------------------------------
#define SPRITESHEET_RES_FILE_LIST						@"StaticData/SpritesheetList.xml"

#define SPRITESHEET_RES_XML_ELEM_TAG_SPRITESHEET		@"spritesheet"
#define SPRITESHEET_RES_XML_ELEM_TAG_BATCHNODE			@"batchnode"

#define SPRITESHEET_RES_XML_ATTR_TAG_FILE				@"file"
#define SPRITESHEET_RES_XML_ATTR_TAG_NAME				@"name"
#define SPRITESHEET_RES_XML_ATTR_TAG_TEXTURE			@"texture"
#define SPRITESHEET_RES_XML_ATTR_TAG_BLENDING			@"blending"
#define SPRITESHEET_RES_XML_ATTR_TAG_VERTEXZ			@"vertexZ"

//=====================================================================================================================
@implementation SpriteSheetRes

@synthesize batchNodeDictionary;

static SpriteSheetRes *sharedRes = nil;

#pragma mark -
#pragma mark Singleton Object Functions
//---------------------------------------------------------------------------------------------------------------------
- (id)init
{
	self = [super init];
	if (self == nil)  {return self;}
	
	return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)dealloc
{
	[batchNodeDictionary release]; batchNodeDictionary = nil;
	
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedRes] retain];
}

//---------------------------------------------------------------------------------------------------------------------
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (id)retain 
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

//---------------------------------------------------------------------------------------------------------------------
- (void)release
{
    //do nothing
}

//---------------------------------------------------------------------------------------------------------------------
- (id)autorelease
{
    return self;
}

#pragma mark Custom Functions
//---------------------------------------------------------------------------------------------------------------------
+ (SpriteSheetRes *)sharedRes
{
	if (sharedRes == nil) {
        sharedRes = [[super allocWithZone:NULL] init];
    }
    return sharedRes;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)loadSpriteSheets
{
	if (batchNodeDictionary != nil) {
		[batchNodeDictionary release];
		batchNodeDictionary = nil;
	}
	batchNodeDictionary = [[NSMutableDictionary alloc] init];
	
	NSString* filePath = [PRFile getFilePath:SPRITESHEET_RES_FILE_LIST];
	[self parseXMLFile:filePath];
}

#pragma mark NSXMLParserDelegate
//---------------------------------------------------------------------------------------------------------------------
- (BOOL)parseXMLFile:(NSString *)filename
{
	//  Load Object List XML file
	NSData *xmlData = [NSData dataWithContentsOfFile:filename];
	if (xmlData == nil)  {
		NSLog(@"[SpriteSheetManager parseXMLFile:filename] Error - Unable to parse file: ""%@""", filename);
		return NO;
	}
	
	NSError* error = nil;
	[self parseXMLData:xmlData parseError:&error];
	
	return (error == nil);
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parseXMLData:(NSData *)data parseError:(NSError **)error
{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
	
    NSError *parseError = [parser parserError];
    if (parseError && error)  {
        *error = parseError;
    }
    [parser release];
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqualToString:SPRITESHEET_RES_XML_ELEM_TAG_SPRITESHEET]) {
		
		NSString* plistFileName = [attributeDict valueForKey:SPRITESHEET_RES_XML_ATTR_TAG_FILE];
		
		NSAssert(plistFileName != nil, @"[SpriteSheetRes parser:didStartElement:namespaceURI:qualifiedName:] Error - Unable to load plist filename");
		
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plistFileName];
	}
	
	if ([elementName isEqualToString:SPRITESHEET_RES_XML_ELEM_TAG_BATCHNODE]) {
		
		NSString* batchnodeName = [attributeDict valueForKey:SPRITESHEET_RES_XML_ATTR_TAG_NAME];
		NSString* textureName = [attributeDict valueForKey:SPRITESHEET_RES_XML_ATTR_TAG_TEXTURE];
		NSNumber* vertexZ = [attributeDict valueForKey:SPRITESHEET_RES_XML_ATTR_TAG_VERTEXZ];
		
		NSAssert(batchnodeName != nil, @"[SpriteSheetRes parser:didStartElement:namespaceURI:qualifiedName:] Error - Unable to load batchnode name");
		NSAssert(textureName != nil, @"[SpriteSheetRes parser:didStartElement:namespaceURI:qualifiedName:] Error - Unable to load texture name");
		
		CCSpriteBatchNode *batchnode = [CCSpriteBatchNode batchNodeWithFile:textureName];
		NSAssert(batchnode != nil, @"[SpriteSheetRes parser:didStartElement:namespaceURI:qualifiedName:] Error - Unable to create CCSpriteBatchNode");
		
		[batchnode setVertexZ:[vertexZ floatValue]];
		[batchNodeDictionary setObject:batchnode forKey:batchnodeName];
		
		//int blending = [attributeDict valueForKey:SPRITESHEET_RES_XML_ATTR_TAG_BLENDING];
	}
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
	
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	
}

@end
